package ru.omsu.diveintoandroid.funnycats.scenes;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

public class GameButton {
	
	public GameButton(CCSprite _sprite){
		sprite = _sprite;
		sprite_normal = _sprite;
	}
	
	public CGPoint GetPosition(){
		return position;
	}
	
	public CCSprite GetSprite(){
		return sprite;
	}
	
	public void SetPosition(CGPoint point){
		position = point;
		
		sprite_normal.setPosition(position);
		sprite_pressed.setPosition(position);
	}
	
	public void SetScale(float _scale){
		scale = _scale;
		sprite_normal.setScale(scale);
		sprite_pressed.setScale(scale);
	}
	
	public void SetPressedSprite(CCSprite _sprite){
		sprite_pressed = _sprite;
	}
	
	public void ClickOnButton(CCLayer L){
		ChangeState();
		SelectSprite(L);
	}
	
	public void AddChildsSprites(CCLayer L){
//		L.addChild(sprite_normal);
//		L.addChild(sprite_pressed);
	}
	
	private void ChangeState(){
		if(isPressed){
			isPressed = false;
		}else{
			isPressed = true;
		}
	}
	
	private void SelectSprite(CCLayer L){
		L.removeChild(sprite, true);
		
		if(isPressed){
			sprite = sprite_pressed;
		}else{
			sprite = sprite_normal;
		}
		
		L.addChild(sprite);
	}
	
	public boolean isPointOnButton(CGPoint point){
		return CGRect.containsPoint(sprite.getBoundingBox(), point);
	}
	
	public void setDefaultState(CCLayer L){
		isPressed = false;
		SelectSprite(L);
	}
	
	public void setAction(CCScene _scene){
		action = _scene;
	}
	
	public void doAction(){
		CCDirector.sharedDirector().runWithScene(action);
	}
	
	private float scale;
	private CGPoint position;
//	private CCTexture2D texture;
	private CCSprite sprite;
	
	private boolean isPressed = false;
	
	private CCSprite sprite_normal;
	private CCSprite sprite_pressed;
	
	private CCScene action = null;
}
